<?php
 
import('Think.Db.Db');
class SqlDump extends Think 
{
private $db;
private $max_size;
public function __construct()
{
$this->db = Db::getInstance();
$this->max_size = conf('DB_VOL_MAXSIZE');
}
private function make_head($vol)
{
$sys_info['os']         = PHP_OS;
$sys_info['web_server'] = $_SERVER['SERVER_SOFTWARE'];
$sys_info['php_ver']    = php_sapi_name();
$sys_info['mysql_ver']  = mysql_get_server_info();
$sys_info['date']       = date('Y-m-d H:i:s');
$head = "-- fanwe SQL Dump Program\r\n".
'-- '.$sys_info['web_server'] ."\r\n".
"-- \r\n".
'-- DATE : '.$sys_info['date']."\r\n".
'-- MYSQL SERVER VERSION : '.$sys_info['mysql_ver']."\r\n".
'-- PHP VERSION : '.$sys_info['php_ver']."\r\n".
'-- Vol : '.$vol."\r\n\r\n\r\n";
return $head;
}
public function dump()
{
$dumptime = get_gmtime();
$tables_all = $this->db->getTables();
$tables = array();
foreach($tables_all as $table)
{
if(preg_match('/'.conf('DB_PREFIX').'/',$table))
{
array_push($tables,$table);
}
}
$vol = 1;
$dumpsql_vol = $this->make_head($vol);
$dumpfile = array();
foreach ($tables as $key=>$tbname)
{
$modelname=str_replace(conf('DB_PREFIX'),'',$tbname);
$tbname_o = $tbname;
$tbname = 	str_replace(conf('DB_PREFIX'),'%DB_PREFIX%',$tbname);
$dumpsql_vol .= "DROP TABLE IF EXISTS `$tbname`;\r\n";
$tmp_arr = $this->db->query("SHOW CREATE TABLE `$tbname_o`");
$tmp_sql = $tmp_arr[0]['Create Table'].";\r\n";
$tmp_sql  = str_replace(conf('DB_PREFIX'),'%DB_PREFIX%',$tmp_sql);
$dumpsql_vol .= $tmp_sql;
$modelname = parse_name($modelname,1);
$model=D($modelname);
$count = $model->count();
if($count>500)
{
$count = ceil($count/500);
if($count>0)
{
for($i=1;$i<=$count;$i++)
{
$limit_str = (($i-1)*500).',500';
$rows = $model->limit($limit_str)->findAll();
if(count($rows)>0)
{
foreach($rows as $row)
{
$dumpsql_row = "INSERT INTO `{$tbname}` VALUES (";
foreach($row as $col_value) 
{
$dumpsql_row .="'".mysql_real_escape_string($col_value)."',";
}
$dumpsql_row=substr($dumpsql_row,0,-1);
$dumpsql_row .= ");\r\n";
$dumpsql_vol.= $dumpsql_row;
}
}
if(strlen($dumpsql_vol)>$this->max_size)
{
$filepath = get_real_path().'public/db_backup/';
$filename = $dumptime.'_'.$vol.'.sql';
$rs = file_put_contents($filepath.$filename,$dumpsql_vol);
if($rs==0)
{
for($ii=1;$ii<=$vol;$ii++)
{
@unlink($filepath.$dumptime.'_'.$ii.'.sql');
}
return false;
}
$vol++;
$dumpsql_vol = $this->make_head($vol);
}
elseif($key==count($tables)-1&&$i==$count-1)
{
$filepath = get_real_path().'public/db_backup/';
$filename = $dumptime.'_'.$vol.'.sql';
$rs = file_put_contents($filepath.$filename,$dumpsql_vol);
if($rs==0)
{
for($ii=1;$ii<=$vol;$ii++)
{
@unlink($filepath.$dumptime.'_'.$ii.'.sql');
}
return false;
}
}
}
}
else
{
if($key==count($tables)-1)
{
$filepath = get_real_path().'public/db_backup/';
$filename = $dumptime.'_'.$vol.'.sql';
$rs = file_put_contents($filepath.$filename,$dumpsql_vol);
if($rs==0)
{
for($ii=1;$ii<=$vol;$ii++)
{
@unlink($filepath.$dumptime.'_'.$ii.'.sql');
}
return false;
}
}
}
}
else
{
if($count>0)
{
for($i=0;$i<$count;$i++)
{
$rows = $model->limit($i.',1')->findAll();
if(count($rows)>0)
{
foreach($rows as $row)
{
$dumpsql_row = "INSERT INTO `{$tbname}` VALUES (";
foreach($row as $col_value) 
{
$dumpsql_row .="'".mysql_real_escape_string($col_value)."',";
}
$dumpsql_row=substr($dumpsql_row,0,-1);
$dumpsql_row .= ");\r\n";
$dumpsql_vol.= $dumpsql_row;
}
}
if(strlen($dumpsql_vol)>$this->max_size)
{
$filepath = get_real_path().'public/db_backup/';
$filename = $dumptime.'_'.$vol.'.sql';
$rs = file_put_contents($filepath.$filename,$dumpsql_vol);
if($rs==0)
{
for($ii=1;$ii<=$vol;$ii++)
{
@unlink($filepath.$dumptime.'_'.$ii.'.sql');
}
return false;
}
$vol++;
$dumpsql_vol = $this->make_head($vol);
}
elseif($key==count($tables)-1&&$i==$count-1)
{
$filepath = get_real_path().'public/db_backup/';
$filename = $dumptime.'_'.$vol.'.sql';
$rs = file_put_contents($filepath.$filename,$dumpsql_vol);
if($rs==0)
{
for($ii=1;$ii<=$vol;$ii++)
{
@unlink($filepath.$dumptime.'_'.$ii.'.sql');
}
return false;
}
}
}
}
else
{
if($key==count($tables)-1)
{
$filepath = get_real_path().'public/db_backup/';
$filename = $dumptime.'_'.$vol.'.sql';
$rs = file_put_contents($filepath.$filename,$dumpsql_vol);
if($rs==0)
{
for($ii=1;$ii<=$vol;$ii++)
{
@unlink($filepath.$dumptime.'_'.$ii.'.sql');
}
return false;
}
}
}
}
}
return true;
}
public function restore($filelist)
{
set_time_limit(0);
$filepath =  get_real_path().'public/db_backup/';
foreach($filelist as $fileItem)
{
$sql = file_get_contents($filepath.$fileItem['filename']);
$sql = $this->remove_comment($sql);
$sql = trim($sql);
$sql = str_replace("\r",'',$sql);
$segmentSql = explode(";\n",$sql);
foreach($segmentSql as $itemSql)
{
if($itemSql!='')
{
$itemSql = str_replace('%DB_PREFIX%',conf('DB_PREFIX'),$itemSql);
$this->db->query($itemSql);
if($this->db->getError()!='')
{
return $this->db->getError();
}
}
}
}
return '';
}
private function remove_comment($sql)
{
$sql = preg_replace('/^\s*(?:--|#).*/m','',$sql);
$sql = preg_replace('/^\s*\/\*.*?\*\//ms','',$sql);
return $sql;
}
}
?>